package com.xiaoyu.lanling.feature.chat.util

import `in`.srain.cube.util.internal.AppCallback
import `in`.srain.cube.util.internal.AppConsumer2
import `in`.srain.cube.util.internal.AppFunction
import com.xiaoyu.base.app.GlobalUI
import com.xiaoyu.base.serverpush.data.ServerPushEventData
import com.xiaoyu.im.client.IMClient
import com.xiaoyu.im.client.proxy.AbstractProxy
import com.xiaoyu.im.datamodel.AppMessage
import com.xiaoyu.im.datamodel.MessagePayload
import com.xiaoyu.im.datamodel.MessagePayload.MessageAlertAction
import com.xiaoyu.im.provider.AbstractMessageDataProvider
import com.xiaoyu.im.provider.ConversationManager
import com.xiaoyu.im.request.flow.RequestFlow
import com.xiaoyu.lanling.feature.chat.data.MessageListData.Companion.instance
import com.xiaoyu.lanling.feature.chat.model.message.MessageType
import com.xiaoyu.lanling.feature.im.ConversationLastContentFunction
import com.xiaoyu.lanling.feature.im.provider.ConversationListenerImpl
import com.xiaoyu.lanling.feature.im.provider.MessageDataProvider
import com.xiaoyu.lanling.feature.im.request.flow.*

object IMInit {

    @JvmStatic
    fun init() {
        val messageDataProvider = initMessageDataProvider()
        initMessageAction()
        initMessageListData(messageDataProvider)
        initConversation()
        initIMProxy()
        initRequestFlow()
        initIMClient(messageDataProvider)
    }

    private fun initMessageAction() {
        MessagePayload.MessageRouteAction.setExecutor {
        }
        MessageAlertAction.setExecutor { action: MessageAlertAction ->
        }
    }

    private fun initIMClient(messageDataProvider: AbstractMessageDataProvider) {
        val receiptFunction: AppConsumer2<String, Long> = AppConsumer2 { chatId: String, time: Long -> instance.setReadReceipt(chatId, time) }
        IMClient.getInstance().init(messageDataProvider, receiptFunction)
    }

    private fun initMessageDataProvider(): AbstractMessageDataProvider {
        val messageDataProvider: AbstractMessageDataProvider = MessageDataProvider.instance
        messageDataProvider.setOnMessageRecallCallback(object : AppCallback<AppMessage> {
            override fun onSuccess(appMessage: AppMessage) {
            }

            override fun onError(e: Throwable) {
                GlobalUI.getInstance().showToast("撤回失败")
            }
        })
        return messageDataProvider
    }

    private fun initMessageListData(messageDataProvider: AbstractMessageDataProvider) {
        instance.init(messageDataProvider)
    }

    private fun initConversation() {
        ConversationManager.init(ConversationLastContentFunction())
        ConversationManager.getInstance().setConversationListener(ConversationListenerImpl())
    }

    private fun initIMProxy() {
        AbstractProxy.sReceiveFunction = AppFunction<AppMessage, AppMessage> { message: AppMessage ->
            if (message.payload.messageType == MessageType.TYPE_02_SERVER_PUSH) {
                ServerPushEventData.getInstance().processEvent(message.payload.attrsCopy);
                ConversationManager.getInstance().readConversation(message.chatToken.chatId)
                return@AppFunction null
            }
            message
        }
    }

    private fun initRequestFlow() {
        RequestFlow.setCheckAction(CheckAction())
        RequestFlow.setPrepareAction(PrepareAction())
        RequestFlow.setSendAction(SendAction())
        RequestFlow.setSuccessAction(SuccessAction())
        RequestFlow.setFailureAction(FailureAction())
    }
}